package com.zyd.module.api.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zyd.common.util.IpUtil;
import com.zyd.common.util.R;
import com.zyd.module.admin.model.*;
import com.zyd.module.admin.service.*;
import com.zyd.module.api.vo.IndexProductTypeVo;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.xml.crypto.Data;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Slf4j
@CrossOrigin
@Api(tags = "主页")
@RestController
@RequestMapping("/index/index")
public class IndexController {

    @Autowired
    private ProductInfosService productInfosService;

    @Autowired
    private ProductTypeService productTypeService;

    @Autowired
    private ProductImageService productImageService;

    @Autowired
    private PictureRoundService pictureRoundService;
    @Autowired
    private BizArticleLookService lookService;


    @PostMapping("/productTypeList")
    public R productList(HttpServletRequest request){
        List<IndexProductTypeVo> productTypeWithImgSrcList = new ArrayList<>();
        List<ProductType> list = productTypeService.list();
        for (ProductType productType : list) {
            QueryWrapper<ProductImage> imageQueryWrapper = new QueryWrapper<>();
            QueryWrapper<ProductInfos> infosQueryWrapper = new QueryWrapper<>();
            infosQueryWrapper.eq("type_id",productType.getId());
            List<ProductInfos> list1 = productInfosService.list(infosQueryWrapper);
            if (list1.size()>0){
                ProductInfos productInfos = list1.get(0);
                imageQueryWrapper.eq("pro_id",productInfos.getId());
                imageQueryWrapper.eq("img_type",1);
                List<ProductImage> list2 = productImageService.list(imageQueryWrapper);
                if(list2.size() > 0){
                    ProductImage productImage = list2.get(0);
                    String proImgSrc = productImage.getProImg();
                    IndexProductTypeVo indexProductTypeVo = new IndexProductTypeVo();
                    indexProductTypeVo.setProductType(productType);
                    indexProductTypeVo.setTypeImgSrc(proImgSrc);
                    productTypeWithImgSrcList.add(indexProductTypeVo);
                }
            }
        }

        log.info("记录访问量");
        Date date=new Date();
        String addr = IpUtil.getIpAddr(request);
        BizArticleLook articleLook = new BizArticleLook();
        articleLook.setArticleId(0);
        articleLook.setUserIp(addr);
        articleLook.setLookTime(date);
        articleLook.setCreateTime(date);
        articleLook.setUpdateTime(date);
        lookService.save(articleLook);

        return R.ok().data("productList",productTypeWithImgSrcList);
    }


    @PostMapping("/getBannerList")
    public R getBannerList(){
        QueryWrapper<PictureRoundEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("is_show",1);
        List<PictureRoundEntity> bannerList = pictureRoundService.list(queryWrapper);
        return R.ok().data("bannerList",bannerList);
    }
}
